/**
 * Created by sun on 2016/8/23.
 */
define(function (require) {
    var app = require('/../js/app');
    require('highchartng');
    var toastr =require('toastr');
    app.useModule('highcharts-ng');

    app.controller('indexController', function($rootScope, $scope, $http, $timeout) {
        $scope.ctrlScope=$scope;
        $scope.ctrlScope.count={};

        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?798cb290357f72a0f29bef8028faa587";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();

        //导出
        $scope.download=function(){

            $http.post("/admin/monthlyReport/stat",{})
                .success(function (data){
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){
                });
        };


        //昨日今日成交量
        $scope.countItem1=function(){
            $http({
                method: 'POST',
                url:"/admin/countInvestAmountByTime",
                data:{},
            }).success(function (data){

                $scope.ctrlScope.count.investAmountYesterday=data.yesterdayAmount;
                $scope.ctrlScope.count.investAmountToday=data.todayAmount;
            }).error(function(data){

            });
        };
        $scope.countItem1();
//后台首页昨日今日注册数量
        $scope.countItem2=function(){
            $http({
                method: 'POST',
                url:"/admin/countUserByTime",
                data:{},
            }).success(function (data){

                $scope.ctrlScope.count.yesterdayNum=data.yesterdayNum;
                $scope.ctrlScope.count.todayNum=data.todayNum;
            }).error(function(data){

            });
        };
        $scope.countItem2();
//后台首页统计用户投资人数，及人次
        $scope.countItem3=function(){
            $http({
                method: 'POST',
                url:"/admin/countInvestNum",
                data:{},
            }).success(function (data){

                $scope.ctrlScope.count.todayInvestNum=data.todayInvestNum;
                $scope.ctrlScope.count.todayInvestPeopleNum=data.todayInvestPeopleNum;
                $scope.ctrlScope.count.yesterdayInvestNum=data.yesterdayInvestNum;
                $scope.ctrlScope.count.yesterdayInvestPeopleNum=data.yesterdayInvestPeopleNum;
            }).error(function(data){

            });
        };
        $scope.countItem3();
//后台首页统计用户账户金额
        $scope.countItem4=function(){
            $http({
                method: 'POST',
                url:"/admin/sumAmount",
                data:{},
            }).success(function (data){

                $scope.ctrlScope.count.usable=data.usable;
            }).error(function(data){

            });
        };
        $scope.countItem4();
//后台首页统计今日明日待还金额
        $scope.countItem5=function(){
            $http({
                method: 'POST',
                url:"/admin/countRepayAmount",
                data:{},
            }).success(function (data){
                console.log(data)


                $scope.ctrlScope.count.todayAmount2=data.todayAmount;
                $scope.ctrlScope.count.tomorrowAmount2=data.tomorrowAmount;
            }).error(function(data){

            });
        };
        $scope.countItem5();
//后台充值金额统计
        $scope.countItem6=function(){
            $http({
                method: 'POST',
                url:"/admin/countRechargeAmount",
                data:{},
            }).success(function (data){
console.log(data)
                $scope.ctrlScope.count.yestodayRechargeAmount=data.yesterdayRechargeAmount;
                $scope.ctrlScope.count.todayRechargeAmount=data.todayRechargeAmount;
            }).error(function(data){

            });
        };
        $scope.countItem6();
//后台首页提现金额统计
        $scope.countItem7=function(){
            $http({
                method: 'POST',
                url:"/admin/countCashAmount",
                data:{},
            }).success(function (data){

                $scope.ctrlScope.count.todayCashAmount=data.todayCashAmount;
                $scope.ctrlScope.count.yesterdayCashAmount=data.yesterdayCashAmount;
            }).error(function(data){

            });
        };
        $scope.countItem7();


        //后台首页每天登陆人数统计
        $scope.countItem8=function(){
            $http({
                method: 'POST',
                url:"/admin/countUserLoginNum",
                data:{},
            }).success(function (data){

                $scope.ctrlScope.count.loginNum=data.loginNum;
            }).error(function(data){

            });
        };
        $scope.countItem8();



        //今日明日锁定自动投余额

        $scope.countItem9=function(){
            $http({
                method: 'POST',
                url:"/admin/countAutoAmount",
                data:{},
            }).success(function (data){
                console.log(data)
                $scope.ctrlScope.count.todayAmount=data.todayAmount;
                $scope.ctrlScope.count.tomorrowAmount=data.tomorrowAmount;
            }).error(function(data){

            });
        };
        $scope.countItem9();


//充值提现统计
        $scope.chartConfig = {
        "options": {
            "chart": {
                defaultSeriesType: 'column',
                //backgroundColor: '#fcfefe'
            }

        },



        xAxis: {
            categories: [],
            title: {text: '时间'},  //Y轴坐标标题  labels:纵柱标尺
            //tickLength: 0,
            startOnTick: false, // y轴坐标是否从某一刻度开始（这个设定与标题无关）
            tickmarkPlacement: 'on',

        },
        //yAxis: {
        //
        //    title: {text: '金额'}  //Y轴坐标标题  labels:纵柱标尺
        //},
            yAxis: [{
                title: {
                    text: '充值金额 / (元)'
                },


            },
                {
                    title: {
                        text: '提现金额 /（元）'
                    },

                    opposite: true
                }

            ],
        tooltip: {
            shared:true,


        },
            plotOptions: {
                area: {
                    fillOpacity: 0.1
                }

            },
        "series": [
            {
                "name": "充值金额",
                "data": [],
                "id": "series-0",
                //"type": "areaspline",
                //fillColor:'#eef8fb',
                //fillOpacity:0.15,
                //color: '#52b7d8',
                color:'#b2dbbf',
                yAxis:0,
                marker: { enabled: false },//曲线上的圆点表示不可见
                tooltip: {
                    valueSuffix: '元'
                }

            },
            {
                "name": "提现金额",
                "data": [],
                "id": "series-1",
                //"type": "areaspline",
                marker: { enabled: false },//曲线上的圆点表示不可见

                color: '#88abda',
                yAxis:1,
                tooltip: {
                    valueSuffix: '元'
                }
            }
        ],
        "title": "充值提现统计",
        "credits": {
            "enabled": false
        },
        "loading": false,
        "size": {}
    };

    //投资趋势图
    $scope.dealChartConfig = {
        colors: ["#eef8fb"],
        "options": {
            "chart": {
                defaultSeriesType: 'column',
                //backgroundColor:'#fffeff'
            }

        },
        xAxis: {
            categories: [],
            title: {text: '时间'},  //Y轴坐标标题  labels:纵柱标尺
            //tickLength: 0,
            startOnTick: false, // y轴坐标是否从某一刻度开始（这个设定与标题无关）
            tickmarkPlacement: 'on'
        },
        //yAxis: {
        //    title: {text: '投资'}  //Y轴坐标标题  labels:纵柱标尺
        //},
        yAxis: [{
            title: {
                text: '投资金额 / （元）'
            },


        },
            {
                title: {
                    text: '投资次数 / （次）'
                },

                opposite: true
            }

        ],
        tooltip: {
            shared:true,
        },
        plotOptions: {
            area: {
                fillOpacity: 0.1
            }

        },
        "series": [
            {
                "name": "投资金额",
                "data": [],
                "id": "series-0",
                //"type": "areaspline",
                //fillOpacity:0.15,
                //color: '#52b7d8',
                color:'#247ba0',
                marker: { enabled: false },//曲线上的圆点表示不可见
                tooltip: {
                    valueSuffix: '元'
                }

            },
            {
                "name": "投资次数",
                "data": [],
                "id": "series-1",
                //"type": "areaspline",
                marker: { enabled: false },//曲线上的圆点表示不可见
                color: '#70c1b3',
                yAxis:1,
                tooltip: {
                    valueSuffix: '次'
                }
            }
        ],
        "title": "投资统计",
        "credits": {
            "enabled": false
        },
        "loading": false,
        "size": {}
    };


        $scope.showChart=function(groupType){
            var params={groupType:groupType};
            $http.post("/admin/getRegionCountRechargeList", params)
                .success(function(data,header,config,status){

                    $scope.chartConfig.series[0].data=data.rechargeDate;
                    $scope.chartConfig.series[1].data=data.cashDate;

                    if(groupType=='month'){
                        $scope.chartConfig.xAxis.categories=[];
                        for(var l=1;l<$scope.chartConfig.series[1].data.length+1;l++){
                            $scope.chartConfig.xAxis.categories.push(l)
                        }
                    }

                }).error(function(data,header,config,status){

                });



            if(groupType=='day'){
                $scope.chartConfig.xAxis.categories=[];
                for(var i=0;i<25;i++){
                    $scope.chartConfig.xAxis.categories.push(i)
                }
            }
            else if(groupType=='week'){
                $scope.chartConfig.xAxis.categories=[];
                for(var j=1;j<8;j++){
                    $scope.chartConfig.xAxis.categories.push(j)
                }
            }   else if(groupType=='year'){
                $scope.chartConfig.xAxis.categories=[];
                for(var z=1;z<13;z++){
                    $scope.chartConfig.xAxis.categories.push(z)
                }
            }


        };

        $scope.showDealChart=function(groupType) {
            var params = {groupType: groupType};
            $http.post("/admin/getRegionCountInvestList", params)
                .success(function (data, header, config, status) {


                    $scope.dealChartConfig.series[0].data = data.investAmountData;
                    $scope.dealChartConfig.series[1].data = data.investNumData;

                    if(groupType=='month'){
                        $scope.dealChartConfig.xAxis.categories=[];
                        for(var l=1;l<data.investAmountData.length+1;l++){
                            $scope.dealChartConfig.xAxis.categories.push(l)
                        }
                    }
                }).error(function (data, header, config, status) {

                });
            if(groupType=='day'){
                $scope.dealChartConfig.xAxis.categories=[];
                for(var i=0;i<25;i++){
                    $scope.dealChartConfig.xAxis.categories.push(i)
                }
            }

            else if(groupType=='week'){
                $scope.dealChartConfig.xAxis.categories=[];
                for(var j=1;j<8;j++){
                    $scope.dealChartConfig.xAxis.categories.push(j)
                }
            }   else if(groupType=='year'){
                $scope.dealChartConfig.xAxis.categories=[];
                for(var z=1;z<13;z++){
                    $scope.dealChartConfig.xAxis.categories.push(z)
                }
            }

        };
        $scope.showChart("day");
        $scope.showDealChart("day");
        $scope.showChart.type=1;
        $scope.showDealChart.type=1;

});
});